﻿namespace TestTool.GUI.Controllers
{
    using System;
    using System.Runtime.CompilerServices;
    using TestTool.GUI.Data;
    using TestTool.GUI.Enums;
    using TestTool.GUI.Views;

    internal class Controller<T> : IController where T: IView
    {
        private TestTool.GUI.Enums.ApplicationState _currentState;
        private readonly T _view;

        public event Action OperationCompleted;

        public event Action OperationStarted;

        public Controller(T view)
        {
            this._view = view;
        }

        public virtual void Initialize()
        {
        }

        public virtual void LoadSavedContext(SavedContext context)
        {
        }

        protected void ReportOperationCompleted()
        {
            if (this.OperationCompleted != null)
            {
                this.OperationCompleted();
            }
        }

        protected void ReportOperationStarted()
        {
            if (this.OperationStarted != null)
            {
                this.OperationStarted();
            }
        }

        public virtual void Stop()
        {
        }

        public virtual void SwitchToState(TestTool.GUI.Enums.ApplicationState state)
        {
            this._currentState = state;
            this.View.SwitchToState(state);
        }

        public virtual void UpdateContext()
        {
        }

        public virtual void UpdateView()
        {
        }

        public virtual void UpdateViewFunctions()
        {
        }

        protected TestTool.GUI.Enums.ApplicationState CurrentState
        {
            get
            {
                return this._currentState;
            }
        }

        public virtual bool RequestPending
        {
            get
            {
                return false;
            }
        }

        protected T View
        {
            get
            {
                return this._view;
            }
        }
    }
}

